;=============================================================================================================
;M,N =>vetores de nb Bytes, nb=16
;Dividir M por N
;Parte inteira e decimal de forma adjacente em MDN
; M->$1000; N->$M+nb; MDN->$N+2*nb
;=============================================================================================================

; export symbols
            ABSENTRY Entry        ; for absolute assembly: mark this as application entry point

Ram equ $1000
Rom equ $4000
Data equ Ram
Code equ Rom

Reset equ   $fffe

Stack_f equ Rom
Stack_i equ Rom-$100

;Interruption Vector         
            ORG   $FFFE
            DC.W  Entry           ; Reset Vector
;Data
nb  equ 16
            org Data            
M   ds.b    nb            
N   ds.b    nb
            org N+2*nb
            
MDN     ds.b    2*nb
aux     ds.b    2
idx     ds.b    1

;Code
            org Code
Entry:
    lds #Stack_f

    clra
    staa idx
        
    ldx #M
    ldy #N

Again:    
    jsr Divide
    
    ldd aux
    std M+idx
    
    ldaa #2
    adda idx
    staa idx
    
    inx
    iny
    
    cpx #(M+nb)
    bne Again    
    bra Sai
    
Divide:
    pshx
    pshy
    pshd
    ;idiv => (d)/(x) => R -> d ; Q ->x
    
    ldab 0,y
    ldaa #0
    std aux
    ldx aux
    ldab 0,x
    ldaa #0
    idiv
    stx aux
    stab aux+2    

    puld        
    puly
    pulx
    rts
        
Sai:    
    bra Entry
    end                
